<script lang="ts" setup>
import { ContentWrap } from '@/components/ContentWrap'
import { ElMessage, type FormInstance } from 'element-plus'

const formRef = ref<FormInstance>()
const visible = ref(false)
const form = reactive({
  fileName: '',
  format: 'mp4',
  bitrate: '2383',
  description: ''
})
const formRules = reactive({
  fileName: [{ required: true, message: '请输入文件名', trigger: ['blur', 'input'] }]
})

const formatOptions = [
  { label: 'mp4', value: 'mp4' },
  { label: 'm3u8', value: 'm3u8' }
]

let rootResolve: any = null
let data = ref(null)
defineExpose({
  open(produceData: any) {
    console.log('🚀 ~ file: produce-video.vue:17 ~ open ~ produceData:', produceData)
    data.value = produceData
    visible.value = true
    return new Promise((resolve) => {
      rootResolve = resolve
    })
  }
})

const onConfirmClick = async () => {
  await formRef.value?.validate((valid) => {
    if (!valid) {
      ElMessage.error('请检查输入是否正确')
      return
    }
    visible.value = false
    rootResolve?.(form)
  })
}

const onCancelClick = () => {
  visible.value = false
}
</script>

<template>
  <el-dialog
    title="创建项目"
    v-model="visible"
    close-on-click-modal
    close-on-press-escape
    width="30%"
  >
    <ContentWrap>
      本次合成视频时长 {{ data.duration }} 秒，将会消耗
      {{ Math.ceil(data.duration * 0.5) }} 元，您当前的余额为 {{ 30 }} 元。
    </ContentWrap>
    <el-form ref="formRef" :model="form" :rules="formRules" label-width="100px">
      <el-form-item label="文件名" prop="fileName">
        <el-input v-model="form.fileName" placeholder="请输入文件名" />
      </el-form-item>
      <el-form-item label="格式">
        <el-select v-model="form.format" placeholder="请选择格式">
          <el-option
            v-for="item in formatOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="码率">
        <el-input v-model="form.bitrate" placeholder="请输入码率" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input type="textarea" v-model="form.description" placeholder="请输入描述" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onConfirmClick">确认</el-button>
        <el-button @click="onCancelClick">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped></style>
